//
//  ContactCell.m
//  Contacts
//
//  Created by 李 雷川 on 14-7-16.
//  Copyright (c) 2014年 李 雷川. All rights reserved.
//

#import "ContactCell.h"
#import "Contact.h"
#import "FileInterface.h"
@implementation ContactCell
@synthesize headImageView,nameLabel,officeLabel,officeNameLabel,phoneLable,phoneNumLabel,callupBtn;
@synthesize contact;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
    [headImageView setContentMode:UIViewContentModeScaleAspectFill];
    [headImageView setClipsToBounds:YES];
    [headImageView.layer setBorderColor:[UIColor whiteColor].CGColor];
    [headImageView.layer setBorderWidth:2.0];
    [headImageView.layer setCornerRadius:headImageView.frame.size.width/2.0];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(void)initWithContact:(Contact*)_contact{
    self.contact = _contact;
    nameLabel.text = contact.name;
    officeNameLabel.text = contact.officeNo;
    phoneNumLabel.text = contact.phone;
    if ([self.contact.name isEqualToString:@"孙宝义"]) {
        NSLog(@"self.contact url is:%@",self.contact.headUrl);
    }
    NSString *fullImagePath = [IMAGE_FORDER stringByAppendingPathComponent:contact.headName];
    if ([[NSFileManager defaultManager]fileExistsAtPath:fullImagePath]) {
        headImageView.image =[UIImage imageWithContentsOfFile:fullImagePath];
    }
    else{
        NSString *fullImagePath = [IMAGE_FORDER stringByAppendingPathComponent:contact.headName];
        if ([[NSFileManager defaultManager]fileExistsAtPath:fullImagePath]) {
             headImageView.image =[UIImage imageWithContentsOfFile:fullImagePath];
        }
        else{
            [  headImageView  setImageWithURL:[NSURL URLWithString:contact.headUrl] placeholderImage:
             [UIImage imageNamed:@"logo.png"]];
        }
    }
}
-(IBAction)onCallup:(id)sender{
    NSString *urlString =[NSString stringWithFormat:@"tel://%@",contact.phone];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    [FileInterface saveContactInfo:contact.userID];
}
@end
